package inspire13.duckhuntroid;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;

public class Crosshair
{
	private int mPosX;
	private int mPosY;
	private int mWidth;
	private int mHeight;
	private float mScaleX;
	private float mScaleY;
	
	private final float mSpeedX = 90;
	private final float mSpeedY = 90;
	
	private float mAccX;
	private float mAccY;
	
	private int mBoundX;
	private int mBoundY;
	
	private Drawable mCrosshair;
	private MediaPlayer shotSound;
	
	private int numOfShots;
	boolean mStarted = false;
	
	public Crosshair(Context context, Drawable sprite, int posX, int posY, int width, int height)
	{
		mPosX = posX;
		mPosY = posY;
		mWidth = width;
		mHeight = height;
		
		mCrosshair = sprite;
		shotSound = MediaPlayer.create(context, R.raw.blast);
		numOfShots = 3;
	}
	
	public boolean Fire(int minX, int minY, int maxX, int maxY)
	{
		if (mStarted)
			shotSound.seekTo(0);
		
		shotSound.start();
		mStarted = true;
		
		numOfShots--;
		
		int mX = mPosX + mWidth;
		int mY = mPosY + mHeight;
		
        if (mX < minX || mY < minY || mPosX > maxX || mPosY > maxY) 
        {
             return false;
        }
            
        return true;
	}
	
	public void SetBoundries(int mX, int mY)
	{
		mBoundX = mX;
		mBoundY = mY;
	}
	
	public int GetNumOfShots()
	{
		return numOfShots;
	}
	
	public void SetAcceleration(float mX, float mY)
	{
		mAccX = mX;
		mAccY = mY;
	}
	
	public void Draw(Canvas canvas)
	{
		int right = (int) ((mPosX + mWidth));
		int bot = (int) ((mPosY + mHeight));
		
		mCrosshair.setBounds(mPosX, mPosY, right, bot);
    	mCrosshair.draw(canvas);
	}
	
	public void Update(float deltaTime)
	{
		if (mPosX >= 0 && (mPosX + mWidth) <= mBoundX)
		{
			mPosX += (mAccX * mSpeedX * deltaTime);
		}
		
		if (mPosX < 0)
		{
			mPosX = 0;
		}
		else if ((mPosX + mWidth) > mBoundX)
		{
			mPosX = mBoundX - mWidth;
		}
		
		if (mPosY >= 0 && (mPosY + mHeight) <= mBoundY)
		{
			mPosY += (mAccY * mSpeedY * deltaTime);
		}
		
		if (mPosY < 0)
		{
			mPosY = 0;
		}
		else if ((mPosY + mHeight) > mBoundY)
		{
			mPosY = mBoundY - mHeight;
		}
	}
	
	public void SetScale(float scaleX, float scaleY)
	{
		mScaleX = scaleX;
		mScaleY = scaleY;
		mWidth =(int)(mWidth * scaleX);
		mHeight =(int)(mHeight * scaleY);
	}
}
